namespace SilverSea.Registration
{
    using System;
    using System.Management;
    using System.Text;

    public static class HardwareInfo
    {
        public static string GetHardwareInfo(string softwareName)
        {
            string str = CryptorEngine.InverseString(RunQuery("Processor", "ProcessorId"));
            softwareName = softwareName + str.Substring(0, str.Length / 2);
            string realHardiskSerialNumber = GetRealHardiskSerialNumber(RunQuery("PhysicalMedia", "SerialNumber"));
            softwareName = softwareName + realHardiskSerialNumber;
            softwareName = softwareName + RunQuery("Processor", "Revision");
            string str3 = RunQuery("DiskDrive", "Model");
            softwareName = softwareName + str3.Substring(str3.Length / 2);
            softwareName = softwareName + str.Substring(str.Length / 2);
            softwareName = CryptorEngine.RemoveUseLess(softwareName);
            if (softwareName.Length < 40)
            {
                return GetHardwareInfo(softwareName);
            }
            return softwareName;
        }

        private static string GetRealHardiskSerialNumber(string diskSerialNumber)
        {
            if (diskSerialNumber != "")
            {
                string str;
                try
                {
                    string[] strArray = new string[diskSerialNumber.Length / 2];
                    for (int i = 0; i < (diskSerialNumber.Length / 2); i++)
                    {
                        strArray[i] = diskSerialNumber.Substring(i * 2, 2);
                    }
                    byte[] buffer = new byte[strArray.Length];
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        if (!byte.TryParse(strArray[j], out buffer[j]))
                        {
                            goto Label_00C8;
                        }
                        buffer[j] = Convert.ToByte(strArray[j], 0x10);
                    }
                    byte[] bytes = new byte[buffer.Length];
                    for (int k = 0; k < buffer.Length; k++)
                    {
                        if ((k != 0) && (((k + 1) % 2) == 0))
                        {
                            bytes[k - 1] = buffer[k];
                        }
                        else
                        {
                            bytes[k + 1] = buffer[k];
                        }
                    }
                    return Encoding.UTF8.GetString(bytes).Trim();
                Label_00C8:
                    str = diskSerialNumber;
                }
                catch
                {
                    goto Label_00D3;
                }
                return str;
            }
        Label_00D3:
            return "";
        }

        private static string RunQuery(string TableName, string MethodName)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_" + TableName);
            foreach (ManagementObject obj2 in searcher.Get())
            {
                try
                {
                    if (obj2[MethodName] != null)
                    {
                        return obj2[MethodName].ToString();
                    }
                    return "";
                }
                catch
                {
                }
            }
            return "";
        }
    }
}

